import apis from "@/api/apis.js"

export default{
	namespaced: true,
	state:{
		userData:{
			"acceptRisk": 0, //风险测评
			"age": 0, //年龄
			"idcard":'',///身份证,
			"idcardName":'',///身份姓名
			"balance": 0, //余额
			"id": 0, ///id
			"integral": 0, //积分
			"nickName": "", //用户名
			"password": "", 
			"risk": 0, //测评
			"salt":'', //头像
			"sex": "", //性别
			"signin": 0,
			"status": 0, ///状态 0,1
			"token": '', 
			"username": "" //账号
		},
		userPass:'', ///保存登录用户密码
	},
	mutations:{
		gettokendata(state,newData){ //保存token
			state.token=newData
		},
		eliminateToken(state){ //清除状态机信息
			state.userData={
			"acceptRisk": 0, //风险测评
			"age": 0, 
			"idcard":'',///身份证,
			"idcardName":'',///身份姓名
			"balance": 0, //余额
			"id": 0,
			"integral": 0, //积分
			"nickName": "", //用户名
			"password": "",
			"risk": 0,
			"salt":'', //头像
			"sex": "",
			"signin": 0,
			"status": 0,
			"token": '',
			"username": "" //账号
			}
		},
		///保存用户密码
		preservation(state,newData){
			state.userPass=newData
		},
		///获取登录用户数据
		getUserData(state,newData){
			state.userData=newData
		},

	},
	actions:{
		///封装判断是否有token方法
		getToken(context){
			return new Promise((resolve,reject ) =>{
				uni.getStorage({
					key:'token',
					success:(res)=> {
						resolve({
							messge:'获取token成功',
							token:res.data,
							code:1
						})
						///有token时保存token到state
						if(res.data){
							context.commit('gettokendata',res.data)
						}
					},
					fail:(err)=> {
						resolve({
							messge:'没有token',
							code:0
						})
					}
				})
			})
		},
		
		///获取用户信息
		async getUser(context,newData){
			const data =await apis.userData.getUserData(newData)
			context.commit('getUserData',data.data)
			return data
		},
		
		////实名认证
		certificationData(context,newData){
			const data = apis.userData.certification(newData)
			return data
		},
		
		//////总收益
		totalRevenueData(context,newData){
			const data = apis.userData.totalRevenue(newData)
			return data
		},
		///黄金收益
		goldIncomeData(context,newData){
			const data = apis.userData.goldIncome(newData)
			return data
		},
		
		///积分兑换余额
		exchangemoney(context,newData){
			const data = apis.userData.pointsexData(newData)
			return data
		}
	}
}